package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author diva
 * @date 2025/10/8 18:21
 * @ProjectName
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public PageResult<Role> page(String name, String label, String remark, Integer page, Integer pageSize) {
        Integer total=roleMapper.count(name,label,remark);
        Integer offset = (page-1)*pageSize;
        List<Role> rows= roleMapper.selectByPage(name,label,remark,offset,pageSize);
        return  PageResult.<Role>builder()
                .total(total)
                .rows(rows)
                .build();
    }

    @Override
    public void addRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }
    @Override
    public void deleteRole(Integer id) {
        roleMapper.deleteById(id);
    }
    @Override
    public Role getRoleById(Integer id) {
        return roleMapper.selectById(id);
    }
    @Override
    public void updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

}
